package com.cgbl.cg.ui.home

import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.*
import com.cgbl.cg.databinding.AppActivitySearchBinding
import com.cgbl.cg.vm.home.SearchViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter

import android.widget.TextView
import com.cgbl.cg.view.FilterPop
import com.lxj.xpopup.XPopup
import com.robot.base.view.CgTextView


@Route(path = AppConstants.Router.HOME.A_SEARCH)
class SearchActivity : GLBaseActivity<AppActivitySearchBinding, SearchViewModel>() {

    private lateinit var searchAllUserAdapter: SearchAllUserAdapter
    private lateinit var worksRecommendAdapter: WorksRecommendAdapter
    private lateinit var recommendCourseAdapter: RecommendCourseAdapter
    private lateinit var searchResourceAdapter: SearchResourceAdapter
    private lateinit var searchProjectAdapter: SearchProjectAdapter
    private lateinit var searchUserAdapter: SearchUserAdapter

    private lateinit var searchTagAdapter: TagAdapter<String>
    private lateinit var hotSearchTagAdapter: TagAdapter<String>

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_search

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initLister()
        initAdapter()
    }

    private fun initLister() {
        binding.editKey.setOnEditorActionListener { v, actionId, event ->
            //当actionId == XX_SEND 或者 XX_DONE时都触发
            //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
            //注意，这是一定要判断event != null。因为在某些输入法上会返回null。
            if (actionId == EditorInfo.IME_ACTION_SEND
                || actionId == EditorInfo.IME_ACTION_DONE
                || (event != null && KeyEvent.KEYCODE_ENTER == event.keyCode && KeyEvent.ACTION_DOWN == event.action)
            ) {
                //处理事件
                viewModel.search()
            }
            return@setOnEditorActionListener false
        }
    }

    private fun initAdapter() {
        searchTagAdapter = object : TagAdapter<String>(viewModel.searchList) {
            override fun getView(parent: FlowLayout, position: Int, s: String?): View {
                val flowLayout = LayoutInflater.from(this@SearchActivity).inflate(
                    R.layout.app_flow_text,
                    binding.flowLayoutRecent, false
                )
                val tv1 = flowLayout.findViewById<CgTextView>(R.id.item_tv_1)
                val tvContent = flowLayout.findViewById<CgTextView>(R.id.item_tv_2)
                tv1.visibility = View.GONE
                tvContent.text = s
                return flowLayout
            }
        }

        binding.flowLayoutRecent.adapter = searchTagAdapter
        binding.flowLayoutRecent.setOnTagClickListener { view, position, parent ->
            viewModel.searchContent.set(viewModel.searchList[position])
            viewModel.search()
            return@setOnTagClickListener true
        }

        hotSearchTagAdapter = object : TagAdapter<String>(viewModel.hotSearchList) {
            override fun getView(parent: FlowLayout, position: Int, s: String?): View {
                val flowLayout = LayoutInflater.from(this@SearchActivity).inflate(
                    R.layout.app_flow_text,
                    binding.flowLayoutRecent, false
                )
                val tv1 = flowLayout.findViewById<CgTextView>(R.id.item_tv_1)
                val tvContent = flowLayout.findViewById<CgTextView>(R.id.item_tv_2)
                tv1.visibility = if (position > 2) {
                    View.GONE
                } else {
                    View.VISIBLE
                }

                tvContent.text = s
                return flowLayout
            }
        }
        binding.flowLayoutHeat.adapter = hotSearchTagAdapter
        binding.flowLayoutHeat.setOnTagClickListener { view, position, parent ->
            viewModel.searchContent.set(viewModel.hotSearchList[position])
            viewModel.search()
            return@setOnTagClickListener true
        }

        searchAllUserAdapter = SearchAllUserAdapter()
        binding.mRvAllUser.apply {
            adapter = searchAllUserAdapter
        }
        searchAllUserAdapter.setNewInstance(arrayListOf("", "", "", ""))

        worksRecommendAdapter = WorksRecommendAdapter(true)
        binding.mRvWork.apply {
            adapter = worksRecommendAdapter
        }
//        worksRecommendAdapter.setNewInstance(arrayListOf("", "", "", ""))

        recommendCourseAdapter = RecommendCourseAdapter()
        binding.mRvCourse.apply {
            adapter = recommendCourseAdapter
        }
//        recommendCourseAdapter.setNewInstance(arrayListOf("", "", "", ""))

        searchResourceAdapter = SearchResourceAdapter()
        binding.mRvResource.apply {
            adapter = searchResourceAdapter
        }

        searchProjectAdapter = SearchProjectAdapter()
        binding.mRvProject.apply {
            adapter = searchProjectAdapter
        }
        searchProjectAdapter.setNewInstance(arrayListOf("", "", "", ""))

        searchUserAdapter = SearchUserAdapter()
        binding.mRvUser.apply {
            adapter = searchUserAdapter
        }
        searchUserAdapter.setNewInstance(arrayListOf("", "", "", ""))
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.searchDataEvent.observe(this){
            searchTagAdapter.notifyDataChanged()
            hotSearchTagAdapter.notifyDataChanged()
        }

        viewModel.uc.filterEvent.observe(this){
            if(it){
                val filterPop = FilterPop(this,viewModel.leftFilter.get()?:"", viewModel.leftList)
                XPopup.Builder(this)
                    .atView(binding.tvFilter1)
                    .asCustom(filterPop)
                    .show()

                filterPop.setConfirmClickListener {
                    filterPop.dismiss()
                    viewModel.leftFilter.set(it)
                }
            }else{
                val filterPop = FilterPop(this,viewModel.rightFilter.get()?:"", viewModel.rightList)
                XPopup.Builder(this)
                    .atView(binding.tvFilter2)
                    .asCustom(filterPop)
                    .show()

                filterPop.setConfirmClickListener {
                    filterPop.dismiss()
                    viewModel.rightFilter.set(it)
                }
            }
        }
    }

}